template.defaults.imports.TimestampToTime = function (timestamp) {
    var date = new Date(timestamp);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
    var Y = date.getFullYear() + '-';
    var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
    var D = date.getDate() + ' ';
    var h = date.getHours() + ':';
    var m = date.getMinutes() + ':';
    var s = date.getSeconds();
    return Y + M + D + h + m + s;
};
template.defaults.imports.CountdownTime = function (factEndDate) {
    var timestamp = (new Date()).valueOf();
    return factEndDate - timestamp;
};
/* global myOrderId*/
/* global getCookie*/
/* global template*/
initData();
function initData(){
    $.get('/antique/api/mallOrder/query', { id:myOrderId,token:getCookie('token') }, function (result) {
       if (result.code == 0){
        var data = result.data;
        document.getElementById('contentTo').innerHTML = template('content', result.data);
        if ( data.receiveDetail && data.receiveDetail.logisticsNo ) {
            $("#logisCpmName").html(data.receiveDetail.logisticsCompany);
            getlogistics(data.receiveDetail.logisticsCode,data.receiveDetail.logisticsNo);
        }
        $('time[sign="unmake"]').each(function () {
            $(this).removeAttr('sign');
            /* global Countdown Aliplayer*/
            Countdown($(this).attr('time'), $(this));
        });
       }
    });
}
function logisticeDetail(){
    $(".topReturn").hide(); 
    $("#select_logistics_detail").popup();
}
function getlogistics(expCode,expNo){
    $.get('/antique/api/logistics/query_logistics', { expCode:expCode,expNo:expNo }, function (result) {
        if (result.code == 0){
            var data = result.data;
            var list = data.Traces;
            if (list.length > 0) {
                $("#odetailExpressDesc").html(list[list.length - 1].AcceptStation );
                $("#oderailExpressTime").html(list[list.length - 1].AcceptTime );
            }
            document.getElementById('contentLogisticsTo').innerHTML = template('contentLogistics', data);
        }
     });
}
function showTop(){
    $(".topReturn").show();
}

function logistics (orderId,sendLogisticsCompany,sendLogisticsNo) {
    $.modal({
      title: "填写物流信息",
      text: '<input type="hidden" id="orderId" value="' + orderId + '" /><input class="inputss" type="text" id="sendLogisticsCompany" value="' + sendLogisticsCompany + '" placeholder="请输入物流公司名字" /><input class="inputss" type="text" id="sendLogisticsNo" value="' + sendLogisticsNo + '" placeholder="请输入快递单号" />',
      buttons: [
        { text: "确定", onClick: function(){
            var sendLogisticsCompany = $("#sendLogisticsCompany").val();
            var sendLogisticsNo = $("#sendLogisticsNo").val();
            var id = $("#orderId").val();
            
             if (sendLogisticsCompany == ''){
                 $.toast("公司不能为空", "cancel");
                 return;
             }
             if (sendLogisticsNo == ''){
                 $.toast("单号不能为空", "cancel");
                 return;
             }
             var params = {};
             params.id = id;
             params.token = getCookie('token');
             params.receiveLogisticsCompany = sendLogisticsCompany;
             params.receiveLogisticsNo = sendLogisticsNo;
        
            $.post('/antique/api/mallOrder/shop/order_update_detail', params, function (result) {
                if (result.code == 0) {
                    sureDeliver (id);
                } else {
                    $.toast(result.message, "cancel");
                }
                });
         } },
        { text: "取消", className: "default" }
      ]
    });
  }
function sureDeliver (orderId) {
    $.post('/antique/api/mallOrder/shop/order_send_goods', { id:orderId,token:getCookie('token') }, function (result) {
        if (result.code == 0) {
            $.toast("操作成功！");
            initData();
        } else {
            $.toast(result.message, "cancel");
        }
        });
     
}
function opRefund (orderId,type) {
    $.confirm("您是否确认该操作？", "", function() {
        $.post('/antique/api/mallOrder/shop/order_refund_audit', { id:orderId,token:getCookie('token'),oper:type }, function (result) {
            if (result.code == 0) {
                $.toast("操作成功！");
                initData();
            } else {
             $.toast(result.message, "cancel");
            }
         });
      }, function() {
        //取消操作
      });
    
}
function finishRefund (orderId) {
    $.confirm("您是否确认完成？", "", function() {
        $.post('/antique/api/mallOrder/shop/order_refund_complate', { id:orderId,token:getCookie('token') }, function (result) {
            if (result.code == 0) {
                $.toast("操作成功！");
                initData();
            } else {
             $.toast(result.message, "cancel");
            }
         });
      }, function() {
        //取消操作
      });
    
}

function upPrice (orderId) {
    $.modal({
        title: "修改价格",
        text: '<input class="weui-input weui-prompt-input"  type="number" id="money"  placeholder="输入想要修改的价格" />',
        buttons: [ 
            { text: "取消", className: "default" },
            { text: "确定", onClick: function(){
               var money = $("#money").val();
              
                var reg = new RegExp("^[0-9]+([.]{1}[0-9]+){0,1}$"); 
                if (!reg.test(money)){
                    $.toast("价格格式错误", "cancel");
                    return;
                }
                $.post('/antique/api/mallOrder/shop/order_modify_money', { id:orderId,token:getCookie('token') ,money : money }, function (result) {
                    if (result.code == 0) {
                        $.toast("修改成功！");
                        initData();
                    } else {
                     $.toast(result.message, "cancel");
                    }
                 });
            } }
            
        ]
    });
       
}